package com.aaa.project.controller;


import com.aaa.entity.UAddr;
import com.aaa.entity.UUserInfo;
import com.aaa.project.mapper.UUserInfoMapper;
import com.aaa.project.service.IUAddrService;
import com.aaa.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.Principal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangxuhui
 * @since 2023-02-01
 */
@RestController
@RequestMapping("/u-addr")
public class UAddrController {

    @Resource
    private IUAddrService addrService;
    @Resource
    private UUserInfoMapper uUserInfoMapper;

    @GetMapping("findAll")
    public Result findAll(Principal principal){
        String name=principal.getName();
        // 根据当前的人的名字用户的id
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("telephone",name);
        UUserInfo uUserInfo=uUserInfoMapper.selectOne(queryWrapper);
        List<UAddr> list = addrService.getAll(uUserInfo.getId());
        return new Result(list);
    }

    @GetMapping("delete/{id}")
    public Result deleted(@PathVariable String id){
       Boolean b= addrService.removeById(id);
        return new Result<>(b);
    }

    @PostMapping("save")
    public Result save(UAddr addr){
        if (addr.getId()==null){
            addr.setIsDeleted(0);
        }
       return new Result<>(addrService.saveOrUpdate(addr));
    }

}

